home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / median < prev    next >
Text File  |  1995-03-20  |  1KB  |  43 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. // Synopsis: Median value
  4.  
  5. // Syntax:   median ( X )
  6.  
  7. // Description:
  8.  
  9. //      median computes the median (middle) value for the input
  10. //      vector/matrix X. If X is a vector, then a single value is
  11. //      returned. If X is a matrix, then a row-vector of the median
  12. //      values for each column is returned.
  13.  
  14. //-------------------------------------------------------------------//
  15.  
  16. median = function ( x )
  17. {
  18.   local (x)
  19.  
  20.   m = x.nr;
  21.   n = x.nc;
  22.  
  23.   x = sort(x).val;
  24.   
  25.   if (m == 1)                     // Row-vector
  26.   {
  27.     if (mod(n,2))                 // n is odd
  28.     {
  29.       y = x[(n+1)/2];
  30.     else                          // n is even
  31.       y = (x[n/2] + x[n/2+1])/2;
  32.     }
  33.   else                            // Column Vector / Matrix
  34.     if (mod(m,2))                 // m is odd
  35.     {
  36.       y = x[(m+1)/2;];
  37.     else                          // m is even
  38.       y = (x[m/2;] + x[m/2+1;])/2;
  39.     }
  40.   }
  41.   return y;
  42. };
  43.